/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myftp.app.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import myftp.app.form.ConnectForm;
import myftp.app.model.ConnexionInfoModel;
import myftp.core.view.AbstractView;
import myftp.core.view.listener.TemplateActionListener;

/**
 *
 * @author CAVARLE Glenn, LUCAS Stéphane
 * Vue de l'utilitaire d'enregistrement et de connexion serveur
 */
public class ServerLoaderView extends AbstractView {

    /**
     * Liste des serveur enregistrés
     * @var serverList
     */
    JList serverlist = null;
    /**
     * Infos du serveur
     * @var serverInfoList
     */
    HashMap<String, ConnexionInfoModel> serverInfoList = null;
    /**
     * Form des infos de connexion
     * @var form
     */
    ConnectForm form = null;

    @Override
    public ServerLoaderView init() {
        this.setLayout(new GridLayout(1, 2));
        this.serverlist = new JList();
        this.form = new ConnectForm();
        this.serverInfoList = new HashMap<String, ConnexionInfoModel>();

        return this;
    }

    /**
     * Ajoute un serveur
     * @param listData
     */
    public void addServerDataList(ArrayList<ConnexionInfoModel> listData) {

        Vector<String> list = new Vector<String>();
        for (ConnexionInfoModel info : listData) {
            this.serverInfoList.put(info.getHostName(), info);
            list.add(info.getHostName());
        }

        this.serverlist.setListData(this.serverInfoList.keySet().toArray());
    }

    /**
     * Ajoute une form abec les infos du serveur choisit
     */
    public void buildForm() {
        JPanel formPanel = new JPanel();
        JButton saveButton = new JButton("Enregistrer");
        saveButton.addActionListener(new BtnSaveListener(this));
        formPanel.setLayout(new GridLayout(5, 1));
        formPanel.add(this.form.hostLabel);
        formPanel.add(this.form.hostField);
        formPanel.add(this.form.loginLabel);
        formPanel.add(this.form.loginField);
        formPanel.add(this.form.passwdLabel);
        formPanel.add(this.form.passwdField);
        formPanel.add(this.form.portLabel);
        formPanel.add(this.form.portField);
        formPanel.add(saveButton);
        this.add(formPanel);
    }

    /**
     * Construit la liste des serveur
     */
    public void buildList() {
        JPanel listPanel = new JPanel();
        JButton btn = new JButton("Connexion");
        btn.addActionListener(new BtnConnectListener(this));
        listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.PAGE_AXIS));
        listPanel.add(new JScrollPane(this.serverlist));
        listPanel.add(btn);
        this.add(listPanel);
    }

    /**
     * Bouton de sauvegarde des serveurs
     */
    private class BtnSaveListener extends TemplateActionListener {

        ServerLoaderView view = null;

        public BtnSaveListener(ServerLoaderView view) {
            super();
            this.view = view;

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ConnectForm form = this.view.form;

            this.controller.setActionParam("form", form);
            this.controller.dispatch("SaveServerAction");
            ConnexionInfoModel info = new ConnexionInfoModel(form.toMap());
            this.view.serverInfoList.put(info.getHostName(), info);
            serverlist.setListData(this.view.serverInfoList.keySet().toArray());

        }
    }

    /**
     * Listener du bouton de connexion au serveur
     */
    private class BtnConnectListener extends TemplateActionListener {

        ServerLoaderView view = null;

        public BtnConnectListener(ServerLoaderView view) {
            super();
            this.view = view;

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ConnexionInfoModel info = this.view.serverInfoList.get((String) this.view.serverlist.getSelectedValue());
            ((JDialog) view.getParentContainer()).dispose();
            this.controller.setActionParam("form", info.toForm());
            this.controller.dispatch("CreateServerAction");
        }
    }
}
